/*
 * @(#)XssSimpleFilterinterceptor.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.interceptor;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.WebWorkStatics;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

import com.hangame.mbl.util.CommonUtil;

import com.nhncorp.lucy.common.util.DataMap;
import com.nhncorp.lucy.common.util.ExtendedMap;
import com.nhncorp.lucy.web.context.ServiceContext;
import com.nhncorp.lucy.web.interceptor.AbstractInterceptor;
import com.nhncorp.lucy.web.interceptor.RequestParamsAware;

// TODO: Auto-generated Javadoc
/**
 * The Class XssSimpleFilterinterceptor.
 *
 * @author NHN
 */
@SuppressWarnings({"serial", "deprecation"})
public class XssSimpleFilterinterceptor extends AbstractInterceptor implements Interceptor, WebWorkStatics {

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.interceptor.AbstractInterceptor#destroy()
	 */
	@Override
	public void destroy() {
	}

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.interceptor.AbstractInterceptor#init()
	 */
	@Override
	public void init() {
	}

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();

		if (action instanceof RequestParamsAware) {
			ExtendedMap params = ServiceContext.getParameterExtMap();
			ExtendedMap tmpParams = new DataMap();

			if (params != null && params.size() > 0) {
				Iterator<Object> it = params.keySet().iterator();

				while (it.hasNext()) {
					String key = (String)it.next();

					if (params.get(key) instanceof String) {
						String value = CommonUtil.xssSimpleFilter(params.getString(key));
						tmpParams.put(key, value); //ConcurrentModificationException 발생가능하여 임시 변수에 저장. iterate구문 내에서 iterate되는 대상이 put되거나 remove되어서는 안됨.
					}
				}

				if (tmpParams != null && tmpParams.size() > 0) {
					Iterator<Object> tmpIt = tmpParams.keySet().iterator();
					while (tmpIt.hasNext()) {
						String key = (String)tmpIt.next();

						if (params.get(key) instanceof String) {
							params.put(key, tmpParams.getString(key));
						}
					}
				}

				((RequestParamsAware)action).setParams(params);
				HttpServletRequest request = (HttpServletRequest)invocation.getInvocationContext().get(HTTP_REQUEST);
				request.setAttribute("params", params);
			}
		}

		return invocation.invoke();
	}

}
